#include "json_protocol.h"
#include "cJSON.h"
#include <string.h>
#include <stdlib.h>

// 简化命令解析
int parse_command(const char* json_str, CommandType* cmd_type) {
    cJSON* root = cJSON_Parse(json_str);
    if (root == NULL) {
        return JSON_PARSE_ERROR;
    }
    
    cJSON* cmd = cJSON_GetObjectItemCaseSensitive(root, "cmd");
    if (!cJSON_IsString(cmd)) {
        cJSON_Delete(root);
        return JSON_MISSING_KEY;
    }
    
    // 根据命令类型确定处理方式
    if (strcmp(cmd->valuestring, "set_time") == 0) {
        *cmd_type = CMD_SET_TIME;
    } 
    else if (strcmp(cmd->valuestring, "fan_control") == 0) {
        *cmd_type = CMD_FAN_CONTROL;
    }
    else if (strcmp(cmd->valuestring, "led_control") == 0) {
        *cmd_type = CMD_LED_CONTROL;
    }
    else if (strcmp(cmd->valuestring, "get_sensor_data") == 0) {
        *cmd_type = CMD_SENSOR_DATA;
    }
    else {
        cJSON_Delete(root);
        return JSON_TYPE_ERROR;
    }
    
    cJSON_Delete(root);
    return JSON_SUCCESS;
}

// 简化风扇命令解析
int parse_fan_command(const char* json_str, FanControl* fan) {
    cJSON* root = cJSON_Parse(json_str);
    if (root == NULL) return JSON_PARSE_ERROR;
    
    cJSON* power = cJSON_GetObjectItemCaseSensitive(root, "power");
    cJSON* speed = cJSON_GetObjectItemCaseSensitive(root, "speed");
    
    if (!cJSON_IsNumber(power) || !cJSON_IsNumber(speed)) {
        cJSON_Delete(root);
        return JSON_MISSING_KEY;
    }
    
    fan->power = power->valueint;
    fan->speed = speed->valueint;
    
    // 验证速度范围
    if (fan->speed < -100 || fan->speed > 100) {
        cJSON_Delete(root);
        return JSON_TYPE_ERROR;
    }
    
    cJSON_Delete(root);
    return JSON_SUCCESS;
}

// 简化LED命令解析
int parse_led_command(const char* json_str, LedControl* led) {
    cJSON* root = cJSON_Parse(json_str);
    if (root == NULL) return JSON_PARSE_ERROR;
    
    cJSON* power = cJSON_GetObjectItemCaseSensitive(root, "power");
    cJSON* brightness = cJSON_GetObjectItemCaseSensitive(root, "brightness");
    
    if (!cJSON_IsNumber(power) || !cJSON_IsNumber(brightness)) {
        cJSON_Delete(root);
        return JSON_MISSING_KEY;
    }
    
    led->power = power->valueint;
    led->brightness = brightness->valueint;
    
    // 验证亮度范围
    if (led->brightness > 100) {
        cJSON_Delete(root);
        return JSON_TYPE_ERROR;
    }
    
    cJSON_Delete(root);
    return JSON_SUCCESS;
}

// 简化时间命令解析
int parse_time_command(const char* json_str, time_t* timestamp) {
    cJSON* root = cJSON_Parse(json_str);
    if (root == NULL) return JSON_PARSE_ERROR;
    
    cJSON* time_val = cJSON_GetObjectItemCaseSensitive(root, "timestamp");
    if (!cJSON_IsNumber(time_val)) {
        cJSON_Delete(root);
        return JSON_MISSING_KEY;
    }
    
    *timestamp = (time_t)time_val->valuedouble;
    cJSON_Delete(root);
    return JSON_SUCCESS;
}

// 简化光照数据创建
char* create_light_data(uint8_t light_value) {
    cJSON* root = cJSON_CreateObject();
    if (root == NULL) return NULL;
    
    cJSON_AddStringToObject(root, "type", "light");
    cJSON_AddNumberToObject(root, "value", light_value);
    cJSON_AddNumberToObject(root, "time", get_timestamp());  // 修改这里
    
    char* json_str = cJSON_PrintUnformatted(root);
    cJSON_Delete(root);
    return json_str;
}

// 简化温湿度数据创建
char* create_dht11_data(float humidity, float temperature) {
    cJSON* root = cJSON_CreateObject();
    if (root == NULL) return NULL;
    
    cJSON_AddStringToObject(root, "type", "dht11");
    
    // 限制浮点数精度，保留1位小数
    char humi_str[16], temp_str[16];
    snprintf(humi_str, sizeof(humi_str), "%.1f", humidity);
    snprintf(temp_str, sizeof(temp_str), "%.1f", temperature);
    
    cJSON_AddStringToObject(root, "humi", humi_str);
    cJSON_AddStringToObject(root, "temp", temp_str);
    cJSON_AddNumberToObject(root, "time", get_timestamp());
    
    char* json_str = cJSON_PrintUnformatted(root);
    cJSON_Delete(root);
    return json_str;
}

// 简化命令响应
char* create_command_response(const char* status, const char* message) {
    cJSON* root = cJSON_CreateObject();
    if (root == NULL) return NULL;
    
    cJSON_AddStringToObject(root, "status", status);
    if (message != NULL) {
        cJSON_AddStringToObject(root, "msg", message);
    }
    
    char* json_str = cJSON_PrintUnformatted(root);
    cJSON_Delete(root);
    return json_str;
}

void free_json_string(char** json_str_ptr) {
    if (json_str_ptr != NULL && *json_str_ptr != NULL) {
        cJSON_free(*json_str_ptr);
        *json_str_ptr = NULL;
    }
}